package com.jing.weibo.service.fans;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.jing.system.model.PageCls;
import com.jing.weibo.mapper.fans.FansAttentionMapper;
import com.jing.weibo.pojo.fans.Fansattention;

/**
 * 用户粉丝或关注的Service
 * @author jing.yue
 * @version 2012/08/31 1.0.0
 */
@Component
public class FansAttentionService {

	@Autowired
	private FansAttentionMapper fansAttentionMapper;

	/**
	 * 根据用户名和粉丝的用户名获取数据库中的信息[不能获取到类型的信息, 有可能获取2条记录]
	 * @param fansattention
	 * @return
	 * @throws Exception
	 */
	public List<Fansattention> findByUsernameFansname(Fansattention fansattention) throws Exception {
		// TODO Auto-generated method stub
		return fansAttentionMapper.findByUsernameFansname(fansattention);
	}

	/**
	 * 根据用户名和粉丝的用户名和类型获取数据库中的信息
	 * @param fansattention
	 * @return
	 * @throws Exception
	 */
	public Fansattention findByUsernameFansnameType(Fansattention fansattention) throws Exception {
		// TODO Auto-generated method stub
		List<Fansattention> list = fansAttentionMapper.findByUsernameFansnameType(fansattention);
		if(list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * 根据用户名和粉丝的用户名和类型修改信息
	 * @param fansattention
	 * @throws Exception
	 */
	@Transactional
	public void updateFansattention(Fansattention fansattention) throws Exception {
		// TODO Auto-generated method stub
		fansAttentionMapper.updateFansattention(fansattention);
	}

	/**
	 * 保存粉丝信息
	 * @param fansattention
	 * @throws Exception
	 */
	@Transactional
	public void saveFansattention(Fansattention fansattention) throws Exception {
		// TODO Auto-generated method stub
		fansAttentionMapper.saveFansattention(fansattention);
	}

	/**
	 * 根据用户和类型删除历史记录
	 * @param fansattention
	 * @throws Exception
	 */
	public void deleteFansattention(Fansattention fansattention) throws Exception {
		// TODO Auto-generated method stub
		fansAttentionMapper.deleteFansattention(fansattention);
	}

	/**
	 * 分页获取用户的单向关注
	 * @param fansattention
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> findOnewayAttention(Fansattention fansattention) throws Exception {
		// TODO Auto-generated method stub
		fansattention.setFollowme("false");
		List<Fansattention> fansattentions = fansAttentionMapper.findAttention(fansattention);
		Integer countSize = fansAttentionMapper.findAttentionCount(fansattention);
		PageCls pageCls = PageCls.setPageCls(fansattention.getPage(), fansattention.getSize(), null, null, countSize, fansattentions.size(), fansattentions);
		return pageCls.getPageInfo();
	}

	/**
	 * 分页获取用户的关注
	 * @param fansattention
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> findAttention(Fansattention fansattention) throws Exception {
		// TODO Auto-generated method stub
		List<Fansattention> fansattentions = fansAttentionMapper.findAttention(fansattention);
		Integer countSize = fansAttentionMapper.findAttentionCount(fansattention);
		PageCls pageCls = PageCls.setPageCls(fansattention.getPage(), fansattention.getSize(), null, null, countSize, fansattentions.size(), fansattentions);
		return pageCls.getPageInfo();
	}

	/**
	 * 根据类型/用户名/关注的用户删除记录
	 * @param fansattention
	 * @throws Exception
	 */
	public void deleteAttentionByTypeUsernameFausername(Fansattention fansattention) throws Exception {
		// TODO Auto-generated method stub
		fansAttentionMapper.deleteAttentionByTypeUsernameFausername(fansattention);
	}

	/**
	 * 分页获取用户的单向粉丝
	 * @param fansattention
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> findOnewayFans(Fansattention fansattention) throws Exception {
		// TODO Auto-generated method stub
		fansattention.setFollowing("false");
		List<Fansattention> fansattentions = fansAttentionMapper.findFans(fansattention);
		Integer countSize = fansAttentionMapper.findFansCount(fansattention);
		PageCls pageCls = PageCls.setPageCls(fansattention.getPage(), fansattention.getSize(), null, null, countSize, fansattentions.size(), fansattentions);
		return pageCls.getPageInfo();
	}

	/**
	 * 分页获取用户的粉丝
	 * @param fansattention
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> findFans(Fansattention fansattention) throws Exception {
		// TODO Auto-generated method stub
		List<Fansattention> fansattentions = fansAttentionMapper.findFans(fansattention);
		Integer countSize = fansAttentionMapper.findFansCount(fansattention);
		PageCls pageCls = PageCls.setPageCls(fansattention.getPage(), fansattention.getSize(), null, null, countSize, fansattentions.size(), fansattentions);
		return pageCls.getPageInfo();
	}

	/**
	 * 根据用户名和粉丝的用户名和类型修改粉丝关系信息
	 * @param fansattention
	 * @throws Exception
	 */
	public void updateFansattentionFollow(Fansattention fansattention) throws Exception {
		// TODO Auto-generated method stub
		fansAttentionMapper.updateFansattentionFollow(fansattention);
	}

}